<?php

namespace Portal\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Portal\NotificationBundle\Entity\Notification;

class MenuController extends Controller
{
    public function topMenuAction()
    {
      $em = $this->getDoctrine()->getManager();

      $menuList = $em->getRepository('PortalMainBundle:Menu')->getAllParentMenuByStatus($this->container->getParameter('statusActive'));
      
      for ($i = 0; $i < count($menuList); $i++) {
        $tmpParentId = $menuList[$i]['id'];
        $child = $em->getRepository('PortalMainBundle:Menu')->getChildByParentId($tmpParentId, $this->container->getParameter('statusActive'));
        if ($child) {
          $childIndex = 0;
          foreach ($child AS $childItem) {
            $menuList[$i]['child'][$childIndex] = $childItem;
            $childIndex++;
          }
        } else {
          $menuList[$i]['child'] = null;
        }
      }
      
      $user = $this->container->get('security.context')->getToken()->getUser();
      if (is_object($user) == TRUE && count($user->getRoles())> 0 )
      {
        $userRoles = $user->getRoles();
      }else{
        $userRoles = null;
      }
      
      return $this->render('PortalMainBundle:Menu:menu.html.twig', array(
                'menuList' => $menuList,
                'userRoles' => $userRoles,
            ));
    }
    
    public function getNotificationsAction()
    {
      $user = $this->container->get('security.context')->getToken()->getUser();
      
      if(is_object($user) == TRUE)
      {
        if(in_array($this->container->getParameter('ROLE_USER'), $user->getRoles()))
        {
          $role = $this->container->getParameter('ROLE_USER') ;
        }else if(in_array($this->container->getParameter('ROLE_TUTOR'), $user->getRoles())){
          $role = $this->container->getParameter('ROLE_TUTOR');
        }
        
        $userNotifications = $this->getDoctrine()->getManager()->getRepository('PortalNotificationBundle:Notification')->getAllActiveNotifications($role, $user->getId(), $this->container->getParameter('notificationStatusUnread'));      
        
        if($userNotifications)
        {
          $count = count($userNotifications);
        }else{
          $count = 0;
        }
        return $this->render('PortalMainBundle:Notification:notification.html.twig', array(
                  'count' => $count,
                  'notification' => $userNotifications,
              ));
      }else{
        $count = 0;
        $userNotifications = false;
        return $this->render('PortalMainBundle:Notification:notification.html.twig', array(
                  'count' => $count,
                  'notification' => $userNotifications,
              ));
      }
    }
}
